package g.beny.framework.web;

import java.io.IOException;
import java.io.InputStream;
import java.util.jar.Manifest;

import org.nutz.conf.NutConf;
import org.nutz.lang.Strings;
import org.nutz.lang.random.R;
import org.nutz.mvc.NutConfig;
import org.nutz.mvc.Setup;

/**
 * mvc容器启动关闭的额外设置
 * @author Administrator
 *
 */
public class MvcSetup implements Setup {

	@Override
	public void destroy(NutConfig arg0) {

	}

	@Override
	public void init(NutConfig nc) {
		//加载系统配置
		NutConf.load("Config.js");
		//获取系统版本号
		nc.getServletContext().setAttribute("version", readVersion(nc));
	}
	
	/**
	 * 获取系统版本号
	 * @methodName: readVersion
	 * @Description:
	 * @CreatedOn: 2012-8-17 下午12:46:58
	 * @author gson
	 * <p>
	 * @param nc
	 * @return
	 */
	private String readVersion(NutConfig nc){
		InputStream input = nc.getServletContext().getResourceAsStream("/META-INF/MANIFEST.MF");
		Manifest mainfest;
		try {
			mainfest = new Manifest(input);
			String version = mainfest.getMainAttributes().getValue("version");
			if(Strings.isEmpty(version))//开发环境没有version随机生成一个
				return "dev-"+R.random(0, 1000);
			return version;
		} catch (IOException e) {
			return "error-"+R.random(0, 1000);
		}
	}
}
